package com.example.baiguangsystem.service.impl;

import com.example.baiguangsystem.entity.Author;
import com.example.baiguangsystem.mapper.AuthorMapper;
import com.example.baiguangsystem.service.AuthorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 作者表service层实现
 *
 * @Author: 27641
 * @ProjectName: BaiGuangSystem
 * @Date: 2022/02/04/14:35
 * @noinspection SpellCheckingInspection
 */
@Service
public class AuthorServiceImpl implements AuthorService {

    /**
     * 依赖注入
     */
    @Autowired
    private AuthorMapper authorMapper;

    /**
     * 根据作者Id查找作者信息
     *
     * @param authorId 需要查找的作者Id
     * @return 查找结果
     */
    @Override
    public Author selectAuthorById(Integer authorId) {
        return this.authorMapper.selectAuthorById(authorId);
    }

    /**
     * 根据作者名字查看作者信息
     *
     * @param authorName 需要查看的作者名字
     * @return 查询结果
     */
    @Override
    public Author selectAuthorByName(String authorName) {
        return this.authorMapper.selectAuthorByName(authorName);
    }

    /**
     * 新增作者业务实现
     *
     * @param author 新增作者信息封装
     */
    @Override
    public int newAddAuthor(Author author) {
        return this.authorMapper.newAddAuthor(author);
    }

    /**
     * 根据作者Id删除作者信息(事务)
     *
     * @param authorId 要删除的作者Id
     * @return 删除作者的信息
     */
    @Override
    @Transactional
    public Author deleteAuthorById(Integer authorId) {
        Author author = this.selectAuthorById(authorId);
        int deleteAuthorNumber = this.authorMapper.deleteAuthorById(authorId);
        /*删除失败*/
        if (deleteAuthorNumber != 1) {
            return null;
        }
        return author;
    }

    /**
     * 根据作者Id修改作者信息
     *
     * @param author 需要修改的作者信息
     * @return 修改成功的条数
     */
    @Override
    public int updateAuthorById(Author author) {
        return this.authorMapper.updateAuthorById(author);
    }
}
